using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{
    public class ControlAnchor : IControlPosition
    {
        // NOTE: relation is not defined in the WindowControl ref-docs,
        // but IS mentioned in the Mod Guide
        public enum ControlRelation
        {
            Absolute,
            Relative,
            Current
        }

        // Shorthand
        private string anchorTo = null;
        private string position = null;
        private string offset = null;
        
        // Full / Override
        private List<Anchor> anchors = new List<Anchor>();
        private ControlRelation relation = ControlRelation.Absolute;
        private Size size = null;

        public ControlAnchor(Element element)
        {
            sourceElement = element;

            Element subElement = element.FindFirstElement("to", false);
            if (subElement != null)
            {
                anchorTo = subElement.Value.Substring(0);
            }

            subElement = element.FindFirstElement("position", false);
            if (subElement != null)
            {
                position = subElement.Value.Substring(0);
            }

            subElement = element.FindFirstElement("offset", false);
            if (subElement != null)
            {
                offset = subElement.Value.Substring(0);
            }

            subElement = element.FindFirstElement("left", false);
            if (subElement != null)
            {
                Anchor left = new Anchor(subElement);
                anchors.Add(left);
            }

            subElement = element.FindFirstElement("top", false);
            if (subElement != null)
            {
                Anchor top = new Anchor(subElement);
                anchors.Add(top);
            }

            subElement = element.FindFirstElement("right", false);
            if (subElement != null)
            {
                Anchor right = new Anchor(subElement);
                anchors.Add(right);
            }

            subElement = element.FindFirstElement("bottom", false);
            if (subElement != null)
            {
                Anchor bottom = new Anchor(subElement);
                anchors.Add(bottom);
            }

            subElement = element.FindFirstElement("size", false);
            if (subElement != null)
            {
                size = new Size(subElement);
            }

            subElement = element.FindFirstElement("relation", false);
            if (subElement != null)
            {
                // Absolute is default.
                if (subElement.Value.ToLower() == "relative")
                {
                    relation = ControlRelation.Relative;
                }
                else if (subElement.Value.ToLower() == "current")
                {
                    relation = ControlRelation.Current;
                }
            }

        }

        #region IControlPosition Members

        private Element sourceElement;
        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element("anchored");

            if (!String.IsNullOrEmpty(anchorTo))
            {
                Element sub = new Element("to", anchorTo);
                sourceElement.ChildElements.Add(sub);
            }

            if (!String.IsNullOrEmpty(position))
            {
                Element sub = new Element("position", position);
                sourceElement.ChildElements.Add(sub);
            }

            if (!String.IsNullOrEmpty(offset))
            {
                Element sub = new Element("offset", offset);
                sourceElement.ChildElements.Add(sub);
            }

            foreach (Anchor anchor in anchors)
            {
                anchor.Commit();
                sourceElement.ChildElements.Add(anchor.SourceElement);
            }

            if (size != null)
            {
                size.Commit();
                sourceElement.ChildElements.Add(size.SourceElement);
            }

            if (relation != ControlRelation.Absolute)
            {
                string relationString = "relative";

                if (relation == ControlRelation.Current)
                {
                    relationString = "current";
                }

                Element sub = new Element("relation", relationString);
                sourceElement.ChildElements.Add(sub);
            }
        }

        #endregion
    }
}
